#include <iostream>
#include "shop.h"
#include "dish.h"

Shop::Shop(int capacity)
    : length(0), capacity(capacity), dishes(new Dish[capacity])
{
}

void Shop::append(const Dish &dish)
{
    if (capacity <= length)
    {
        return;
    }
    dishes[length] = dish;
    length++;
}

float Shop::sumOfBusiness()
{
    float sum = 0;
    for (int i = 0; i < length; i++)
    {
        sum += dishes[i].business();
    }
    return sum;
}

float Shop::largestProfit()
{
    float max = 0, p;
    for (int i = 0; i < length; i++)
    {
        p = dishes[i].profit();
        if (p > max)
        {
            max = p;
        }
    }
    return max;
}

int Shop::largestSales()
{
    int max = 0, s;
    for (int i = 0; i < length; i++)
    {
        s = dishes[i].getSales();
        if (s > max)
        {
            max = s;
        }
    }
    return max;
}

void Shop::range(bool (*f)(int length, Dish &value))
{
    bool ok;
    for (int i = 0; i < length; i++)
    {
        ok = f(i, dishes[i]);
        if (!ok)
        {
            break;
        }
    }
}

void Shop::sort()
{
    Dish temp;
    for (int i = 0; i < length-1; i++)
    {
        for (int j = i + 1; j < length; j++)
        {
            if (dishes[i].profit() < dishes[j].profit())
            {
                temp = dishes[i];
                dishes[i] = dishes[j];
                dishes[j] = temp;
            }
        }
    }
}
void Shop::display()
{
    Dish *tmp;
    std::cout << "Name\tSales\tImportPrice\tSoldPrice\t" << std::endl;
    for (int i = 0; i < length; i++)
    {
        tmp = &dishes[i];
        std::cout << tmp->getName() << "\t"
                  << tmp->getSales() << "\t"
                  << tmp->getImportPrice() << "\t"
                  << tmp->getSoldPrice() << "\t" << std::endl;
    }
}